package com.opesoft.calendar.biz;

import java.util.Calendar;

public class Month
{
	protected int year;
	protected int month;
	protected Calendar calendar;
	protected int[] now=new int[3];

	public Month()
	{
		this.calendar=Calendar.getInstance();
		{
			now[0]=this.calendar.get(Calendar.YEAR);
			now[1]=this.calendar.get(Calendar.MONTH);
			now[2]=this.calendar.get(Calendar.DAY_OF_MONTH);
			this.year=now[0];
			this.month=now[1];
		}
		this.calendar.set(Calendar.DAY_OF_MONTH,1);
	}

	public Month(int year,int month)
	{
		this.year=year;
		this.month=month;
		this.calendar=Calendar.getInstance();
		{
			now[0]=this.calendar.get(Calendar.YEAR);
			now[1]=this.calendar.get(Calendar.MONTH);
			now[2]=this.calendar.get(Calendar.DAY_OF_MONTH);
		}
		this.calendar.set(year,month,1);
	}

	/**
	 * 一共有几周
	 * 
	 * @return
	 */
	public int weeks()
	{
		return this.calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
	}

	/**
	 * 这个月有多少天
	 * 
	 * @return
	 */
	public int daysOfMonth()
	{
		return this.calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 上一个月有多少天
	 * 
	 * @return
	 */
	public int daysOfBeforeMonth()
	{
		return this.beforeMonth().getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public Calendar beforeMonth()
	{
		int year=this.year;
		int month=this.month-1;
		if(month<0)
		{
			month=11;
			year--;
		}

		Calendar calendar=Calendar.getInstance();
		calendar.set(year,month,1);
		return calendar;
	}

	public Calendar afterMonth()
	{
		int year=this.year;
		int month=this.month+1;
		if(month>11)
		{
			month=0;
			year++;
		}

		Calendar calendar=Calendar.getInstance();
		calendar.set(year,month,1);
		return calendar;
	}

	/**
	 * 把数字月份转成两位字符串
	 * 
	 * @param month
	 * @return
	 */
	public String monthStr(int month)
	{
		String r=month+"";
		if(r.length()<2) return "0"+r;

		return r;
	}

	/**
	 * 第一天是周几
	 * 
	 * @return
	 */
	public int firstDayOfWeek()
	{
		return this.calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 最后一天是周几
	 * 
	 * @return
	 */
	public int lastDayOfWeek()
	{
		int i=(this.daysOfMonth()-(8-this.firstDayOfWeek()))%7;
		return i==0?7:i;
	}

	public int[][] matrixDays()
	{
		int[][] r=new int[7][];
		int firstDayOfWeek=this.firstDayOfWeek();
		int lastDayOfWeek=this.lastDayOfWeek();

		if(firstDayOfWeek>1)
		{
			Calendar bm=this.beforeMonth();
			int year=bm.get(Calendar.YEAR);
			int month=bm.get(Calendar.MONTH);
			int endDay=bm.getActualMaximum(Calendar.DAY_OF_MONTH);
			int startDay=endDay-firstDayOfWeek+2;

			r[0]=new int[]{year,month,startDay};
			r[1]=new int[]{year,month,endDay};
		}

		r[2]=new int[]{this.year,this.month,1};
		r[3]=new int[]{this.year,this.month,this.daysOfMonth()};

		if(lastDayOfWeek<7)
		{
			Calendar am=this.afterMonth();
			int year=am.get(Calendar.YEAR);
			int month=am.get(Calendar.MONTH);

			r[4]=new int[]{year,month,1};
			r[5]=new int[]{year,month,7-lastDayOfWeek};
		}

		r[6]=this.now;

		return r;
	}
}
